package com.shenmazong.servertalkadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.pojo.TbClass;
import com.shenmazong.pojo.TbSeller;
import com.shenmazong.pojo.TbSellerFeeRecord;
import com.shenmazong.servertalkadmin.mapper.TbAdminMapper;
import com.shenmazong.servertalkadmin.mapper.TbClassMapper;
import com.shenmazong.servertalkadmin.mapper.TbSellerFeeRecordMapper;
import com.shenmazong.servertalkadmin.mapper.TbSellerMapper;
import com.shenmazong.servertalkadmin.service.TbSellerFeeRecordService;
import com.shenmazong.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class TbSellerFeeRecordServiceImpl implements TbSellerFeeRecordService {
    @Autowired
    TbSellerFeeRecordMapper tbSellerFeeRecordMapper;

    @Autowired
    TbSellerMapper tbSellerMapper;

    @Autowired
    TbClassMapper tbClassMapper;

    @Override
    public ResponseResult addTbSellerFeeRecord(Integer sellerId, Integer teacherId, Integer studentId,
                                               Integer classId, Long changeFee, String changeReason) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //査上一个记录的最终提成
        TbSeller seller = tbSellerMapper.selectById(sellerId);
        if(seller == null) {
            result.setFailMessage("销售人员不存在");
            return result;
        }

        //--0未上课1点名2调课3删除4结算
        TbClass tbClass = tbClassMapper.selectById(classId);
        if(tbClass.getClassStatus() != 1) {
            result.setFailMessage("提成不能结算");
            return result;
        }

        //添加记录
        TbSellerFeeRecord record = new TbSellerFeeRecord();
        record.setSellerId(sellerId);
        record.setTeacherId(teacherId);
        record.setStudentId(studentId);
        record.setClassId(classId);
        record.setChangeFee(changeFee);
        //计算并添加最终金额
        record.setSellerFee(seller.getSaleFee()+changeFee);
        record.setChangeReason(changeReason);
        tbSellerFeeRecordMapper.insert(record);
        if(record.getId() == null) {
            result.setFailMessage("生成提成记录失败");
            return result;
        }

        //-- 修改教师提成余额
        seller.setSaleFee(seller.getSaleFee()+changeFee);
        tbSellerMapper.updateById(seller);

        //-- 修改课时结算状态
        tbClass.setClassStatus(4);
        tbClassMapper.updateById(tbClass);

        //-- 返回结果
        resultMap.put("record",record);
        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult delTbSellerFeeRecord(Integer id) {
        ResponseResult result = ResponseResult.SUCCESS();

        return result;
    }

    @Override
    public ResponseResult getTbSellerFeeRecord(Integer pageCurrent, Integer pageSize) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        Page<TbSellerFeeRecord> page = new Page<>(pageCurrent, pageSize);
        IPage<TbSellerFeeRecord> pages = tbSellerFeeRecordMapper.selectPage(page, null);


        List<TbSellerFeeRecord> tbSellerFeeRecord = tbSellerFeeRecordMapper.selectList(null);

       resultMap.put("pages", pages);
       result.setResult(resultMap);

       return result;
    }
}
